package com.label.Thread;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileWriterThread {

    private FileWriter gantryWriter;
    private BufferedWriter gantryBufferedWriter;

    private Map<String,String> resRoad;

    public FileWriterThread() throws IOException {
        File newfile = new File("C:\\Users\\75046\\Desktop\\gantry.txt");
        if (!newfile.exists()) {
            newfile.createNewFile();
        }
        this.gantryWriter = new FileWriter(newfile, true);
        this.gantryBufferedWriter = new BufferedWriter(gantryWriter);

        resRoad=new HashMap<>();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        gantryBufferedWriter.close();
        gantryWriter.close();
    }

    public  synchronized  void  writeGantry(String str) throws IOException {
        gantryBufferedWriter.write(str);
    }

    public  synchronized  void  writeRoad(String key,String value) throws IOException {
        if(!resRoad.containsKey(key)){
            resRoad.put(key,value);
        }
    }

    public Map<String,String> getResRoad(){
        return this.resRoad;
    }

    public void closeFile() throws IOException {
        gantryBufferedWriter.close();
        gantryWriter.close();
    }

}
